
<%
	{
		/*<!-- Initiation -->*/
		response.setContentType("text/html; charset=UTF-8");
		canss.web.controls.WebDBControl webdbcontrol = null;
		webdbcontrol = (webdbcontrol == null) ? new canss.web.controls.WebDBControlBean() : webdbcontrol;

		/*<!-- Preparing inputs -->*/
		int i = 0;

		/*<!-- Query Compilation -->*/
		canss.supports.Makequery_select query = new canss.supports.Makequery_select().Select("canssevent_extradata").PushFrom("tblEvent").PushWhere("tblMID_mid='130'").PushWhere("tblEventDescription_eventid='401'");

		java.util.Iterator<String[]> iter = null;
		java.util.Hashtable<String, Integer> hash = null;
		try
		{
			java.util.LinkedList<String[]> res = webdbcontrol.Select(query);
			iter = res.iterator();
			if (iter.hasNext())
			{
				String[] curhashtmp = iter.next();
				hash = new java.util.Hashtable<String, Integer>(curhashtmp.length);
				for (i = 0; i < curhashtmp.length; i++)
					hash.put(curhashtmp[i], i);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}

		/*<!-- Parsing preparation -->*/
		String pattern1 = ".*<c-status>\\s?(\\d+)\\s?<\\/c-status>.*";
		String pattern2 = ".*<cs[(]Referer[)]>\\s?(.+)\\s?<\\/cs[(]Referer[)]>.*";
		String pattern2_2 ="^\\s?http:\\/\\/?([a-zA-Z0-9.:]+)\\/.*";
		String pattern3 = ".*<c-os>\\s?(.+)\\s?<\\/c-os>.*";
		String pattern4 = ".*<cs[(]User-Agent[)]>\\s?" + "(.+)" + "\\s?<\\/cs[(]User-Agent[)]>.*";
		String pattern5_1 = ".*<sc-bytes>\\s?(.+)\\s?<\\/sc-bytes>.*";
		String pattern5_2 = ".*<filesize>\\s?(.+)\\s?<\\/filesize>.*";

		java.util.regex.Pattern p1 = java.util.regex.Pattern.compile(pattern1);
		java.util.regex.Pattern p2 = java.util.regex.Pattern.compile(pattern2);
		java.util.regex.Pattern p2_2 = java.util.regex.Pattern.compile(pattern2_2);
		java.util.regex.Pattern p3 = java.util.regex.Pattern.compile(pattern3);
		java.util.regex.Pattern p4 = java.util.regex.Pattern.compile(pattern4);
		java.util.regex.Pattern p5_1 = java.util.regex.Pattern.compile(pattern5_1);
		java.util.regex.Pattern p5_2 = java.util.regex.Pattern.compile(pattern5_2);

		/*<!-- Data processing -->*/
		//play/fail ratio : c-status( 200 / all )
		double totalcount = 0;
		double succcount = 0;
		//Client : cs(Referer)
		java.util.Hashtable<String, Integer> cs_list = new java.util.Hashtable<String, Integer>();
		//Client OS: c-os
		java.util.Hashtable<String, Integer> c_os_list = new java.util.Hashtable<String, Integer>();
		//Client app: cs(User-Agent)
		java.util.Hashtable<String, Integer> csref_list = new java.util.Hashtable<String, Integer>();
		//content avg play ratio: sc-bytes/filesize
		double sc_bytes = 0;
		double filesize = 0;

		//processing
		java.util.regex.Matcher m = null;
		while (iter != null && iter.hasNext())
		{
			String[] cur = iter.next();
			String tmp = cur[hash.get("canssevent_extradata")];
			//
			m = p1.matcher(tmp);
			while (m.find())
			{
				if ("200".equalsIgnoreCase(m.replaceAll("$1")))
					succcount += 1;
				totalcount += 1;
			}
			//
			m = p2.matcher(tmp);
			while (m.find())
			{
				String a = m.replaceAll("$1");
				
				java.util.regex.Matcher m2 = p2_2.matcher(a);
				if(m2.find())
				{
					a = m2.replaceAll("$1");
					if (cs_list.containsKey(a))
					{
						Integer b = cs_list.remove(a);
						b++;
						cs_list.put(a, b);
					} else
						cs_list.put(a, new Integer(1));
				}
			}
			//
			m = p3.matcher(tmp);
			while (m.find())
			{
				String a = m.replaceAll("$1");
				if (c_os_list.containsKey(a))
				{
					Integer b = c_os_list.remove(a);
					b++;
					c_os_list.put(a, b);
				} else
					c_os_list.put(a, new Integer(1));
			}
			//
			m = p4.matcher(tmp);
			while (m.find())
			{
				//out.write(m.group());
				String a = m.replaceAll("$1");
				String c[] = a.split("_");
				if (c != null && c.length > 0 && csref_list.containsKey(c[0]))
				{
					Integer b = csref_list.remove(c[0]);
					b++;
					csref_list.put(c[0], b);
				} else
					csref_list.put(c[0], new Integer(1));
			}
			//
			m = p5_1.matcher(tmp);
			while (m.find())
			{
				String a = m.replaceAll("$1");
				try
				{
					if(canss.supports.basic.isNotEmpty(a))
						sc_bytes += Double.parseDouble(a);
				}catch(NumberFormatException e)
				{
					sc_bytes = 0;
				}
			}
			m = p5_2.matcher(tmp);
			while (m.find())
			{
				String a = m.replaceAll("$1");
				try
				{
					if(canss.supports.basic.isNotEmpty(a))
						filesize += Double.parseDouble(a);
				}catch(NumberFormatException e)
				{
					filesize = 1;
				}
			}
		}
		if (totalcount == 0)
			totalcount = 1;
		if(filesize==0)
			filesize = 1;

		/*<!-- Data representation -->*///format: [ [['',''],['',12]...], ~~ ]
		out.write("[");
		//play/fail ratio : c-status( 200 / all )
		out.write(String.format("[['ratio',%f]]", (succcount / totalcount)));
		out.write(",");

		//Client : cs(Referer)
		out.write("[");
		java.util.Enumeration<String> cs_list_enumkey = cs_list.keys();
		while (cs_list_enumkey.hasMoreElements())
		{
			String key = cs_list_enumkey.nextElement();
			out.write(String.format("['%s',%d]", key, cs_list.get(key)));
			if (cs_list_enumkey.hasMoreElements())
				out.write(",");
		}
		out.write("]");
		out.write(",");
		
		//Client OS: c-os
		out.write("[");
		java.util.Enumeration<String> c_os_list_enumkey = c_os_list.keys();
		while (c_os_list_enumkey.hasMoreElements())
		{
			String key = c_os_list_enumkey.nextElement();
			out.write(String.format("['%s',%d]", key, c_os_list.get(key)));
			if (c_os_list_enumkey.hasMoreElements())
				out.write(",");
		}
		out.write("]");
		out.write(",");
		
		//Client app: cs(User-Agent)
		out.write("[");
		java.util.Enumeration<String> csref_list_enumkey = csref_list.keys();
		while (csref_list_enumkey.hasMoreElements())
		{
			String key = csref_list_enumkey.nextElement();
			out.write(String.format("['%s',%d]", key, csref_list.get(key)));
			if (csref_list_enumkey.hasMoreElements())
				out.write(",");
		}
		out.write("]");
		out.write(",");
		
		//content avg play ratio: sc-bytes/filesize
		out.write(String.format("[['avg_played',%f]]", (sc_bytes / filesize)));
		//
		out.write("]");
	}
%>